<template>
  <rm-layout-top splitcolor="white" :splitwidth="5" :fixsize="70" :canDrag="false">
      <template v-slot:fix>
          <div style="height: 70px; border: 1px solid #ccc">
              <div class="search-title">
                  <table style="width: 100%">
                      <tr>
                          <!-- <td>菜单</td>
                          <td>
                              <el-select v-model="searchobj.prgcode" filterable clearable @keyup.enter.native="selectBlur()" ref="ddlprgcodeSelect">
                                  <el-option v-for="(item,index) in prgcodeOption" :key="index" :label="item.label" :value="item.value">
                                      <span style="float: left">{{ item.label }}</span>
                                      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                  </el-option>
                              </el-select>
                          </td> -->
                          <td>类别</td>
                          <td>
                              <el-select v-model="searchobj.reportkind" filterable clearable @change="DataFilter">
                                  <el-option v-for="(item, index) in reportkindOption" :key="index" :label="item.label" :value="item.value"></el-option>
                              </el-select>
                          </td>
                          <td>快速检索</td>
                          <td>
                              <el-input v-model="searchobj.reportname" placeholder="统计报表ID或名称" @keyup.enter.native="DataFilter" clearable @clear="DataFilter"> </el-input>
                          </td>
                      </tr>
                  </table>
              </div>
              <div class="top-deal">
                  <el-button @click.native="btnQuery" icon="el-icon-search" type="primary" :disabled="changeflag">查询</el-button>
                  <el-dropdown style="padding-left: 5px; padding-right: 5px">
                      <el-button icon="el-icon-document">转移到默认菜单</el-button>
                      <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="ExChangereportid(Form_StatReport.statreportid, '1')">标本类统计</el-dropdown-item>
                          <el-dropdown-item @click.native="ExChangereportid(Form_StatReport.statreportid, '2')">报告类统计</el-dropdown-item>
                          <el-dropdown-item @click.native="ExChangereportid(Form_StatReport.statreportid, '3')">结果类统计</el-dropdown-item>
                      </el-dropdown-menu>
                  </el-dropdown>
                  <el-button icon="el-icon-plus" @click.native="btnAdd" :disabled="changeflag">新增</el-button>
                  <el-button icon="el-icon-delete" @click.native="btnDelete" :disabled="changeflag">删除</el-button>
                  <el-button icon="el-icon-document" @click.native="btnSave" :disabled="!changeflag">保存</el-button>
                  <el-button icon="el-icon-close" @click.native="btnCancel" :disabled="!changeflag">取消</el-button>
                  <el-button icon="el-icon-s-tools" @click="btnSetStyle" :disabled="changeflag">样式设置</el-button>
                  <el-dropdown style="padding-left: 5px; padding-right: 5px">
                      <el-button icon="el-icon-document">导出</el-button>
                      <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="btnExport('2')">报表</el-dropdown-item>
                          <el-dropdown-item @click.native="btnExport('1')">仅报表样式</el-dropdown-item>
                          <el-dropdown-item @click.native="btnExport('0')">导出全部</el-dropdown-item>
                      </el-dropdown-menu>
                  </el-dropdown>
                  <el-dropdown style="padding-left: 5px; padding-right: 5px">
                      <el-button icon="el-icon-document">导入</el-button>
                      <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="btnImport('2')">报表</el-dropdown-item>
                          <el-dropdown-item @click.native="btnImport('1')">仅报表样式</el-dropdown-item>
                          <el-dropdown-item @click.native="btnImport('0')">导入全部</el-dropdown-item>
                          <el-dropdown-item @click.native="btnImport('3')">导入全部样式</el-dropdown-item>
                      </el-dropdown-menu>
                  </el-dropdown>
              </div>
          </div>
      </template>
      <rm-layout-right splitcolor="white" movingcolor="#00FFFF" :splitwidth="5" :fixsize="800">
          <template v-slot:fix>
              <div style="height: 100%; text-align: center">
                  <rm-layout-top splitcolor="white" :splitwidth="5" :fixsize="290" :canDrag="false">
                      <template v-slot:fix>
                          <div style="height: 290px; border: 1px solid #ccc">
                              <el-form :inline="true" @submit.native.prevent label-width="auto" ref="form_jbxx" :model="Form_StatReport" :disabled="nodata">
                                  <table style="width: 100%">
                                      <tr>
                                          <td align="right">
                                              <font color="#F56C6C">报表ID</font>
                                          </td>
                                          <td>
                                              <el-input v-model="Form_StatReport.statreportid" ref="input1" @keyup.enter.native="nextFocus(1)" placeholder="请输入4001-5000之间的值" @change="verifyReportId" :disabled="true"></el-input>
                                          </td>
                                          <td align="right">
                                              <font color="#F56C6C">所属菜单</font>
                                          </td>
                                          <td colspan="3">
                                              <el-input v-model="Form_StatReport.prgcode" tyle="width:100%" @keyup.enter.native="nextFocus(2)" @change="hasChange" ref="input2" disabled></el-input>
                                              <!-- <el-select v-model="Form_StatReport.prgcode" filterable clearable ref="input2" style="width:100%" @keyup.enter.native="nextFocus(2)" @change="hasChange" remote reserve-keyword :remote-method="remoteMethod" :loading="loading">
                                                  <el-option v-for="(item,index) in secsysprgOption" :key="index" :label="item.label" :value="item.value">
                                                      <span style="float: left">{{ item.label }}</span>
                                                      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                                  </el-option>
                                              </el-select> -->
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="right">
                                              <font color="#F56C6C">报表名称</font>
                                          </td>
                                          <td>
                                              <el-input v-model="Form_StatReport.reportname" ref="input3" @keyup.enter.native="nextFocus(5)" clearable @change="hasChange"></el-input>
                                          </td>
                                          <td align="right">序号</td>
                                          <td>
                                              <el-input-number v-model="Form_StatReport.seq" ref="input4" @keyup.enter.native="nextFocus(4)" clearable :precision="0" :step="10" :min="10" style="width: 100%" @change="hasChange"></el-input-number>
                                          </td>
                                          <td align="right">连接配置</td>
                                          <td>
                                              <el-select v-model="Form_StatReport.connectioncode" filterable clearable ref="input9" @change="hasChange" @keyup.enter.native="nextFocus(9)">
                                                  <el-option v-for="(item,index) in datebaseConfigOption" :key="index" :label="item.label" :value="item.value">
                                                      <span style="float: left">{{ item.label }}</span>
                                                      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                                                  </el-option>
                                              </el-select>
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="right">
                                              <font color="#F56C6C">报表类别</font>
                                          </td>
                                          <td>
                                              <el-autocomplete popper-class="my-autocomplete" v-model="Form_StatReport.reportkind" :fetch-suggestions="querySearch" placeholder="请输入内容" @focus="reportkindInit" clearable style="width: 100%" ref="input5" @keyup.enter.native="nextFocus(3)" @change="hasChange"> </el-autocomplete>
                                          </td>

                                          <td align="right">上级菜单</td>
                                          <td style="text-align: left" colspan="3">
                                              <el-input v-model="Form_StatReport.parentcode" ref="input6" @keyup.enter.native="nextFocus(6)" clearable @change="hasChange" @dblclick.native="ShowInMenu ? (dialogmenuVisible = true) : ''" style="width: 60%; margin-right: 5px" :disabled="!ShowInMenu"></el-input>
                                              <el-checkbox v-model="ShowInMenu">自定义菜单位置</el-checkbox>
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="right">统计SQL</td>
                                          <td colspan="5">
                                              <el-input type="textarea" v-model="Form_StatReport.reportsql" :rows="5" ref="input7" @keyup.enter.native="nextFocus(7)" @change="hasChange" @dblclick.native="OpenTestSql()" style="width: calc(100% - 140px); float: left"></el-input>
                                              <div style="width: 130px; color: blue; float: right; text-align: left">
                                                  特殊代号:<br />
                                                  @userno 当前登录用户<br />
                                                  @hospcode 当前医院<br />
                                              </div>
                                          </td>
                                      </tr>
                                      <tr>
                                          <td align="right">报表说明</td>
                                          <td colspan="5">
                                              <el-input type="textarea" v-model="Form_StatReport.description" :rows="3" ref="input8" @keyup.enter.native="nextFocus(8)" @change="hasChange"></el-input>
                                          </td>
                                      </tr>
                                  </table>
                              </el-form>
                          </div>
                      </template>
                      <div style="height: 100%; width: 100%;height: calc(100% - 32px);">
                          <vxe-table :data.sync="rxConditionData" :loading="right_loading" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" border height="auto" highlight-current-row highlight-hover-column highlight-hover-row :row-class-name="CondRowClassName" :cell-class-name="cellClassName" how-header-overflow ref="rxTable" resizable show-overflow="ellipsis" size="mini" @cell-dblclick="DblclickOpen" @current-change="changecurrclick" :edit-config="{ trigger: 'click', mode: 'cell', activeMethod: activeMethod, showStatus: true, showIcon: false }">
                              <vxe-table-column field="seq" title="序号" width="50" header-align="center" :edit-render="{ name: 'input', autoselect: true, events: { change: hasChange } }" align="center"></vxe-table-column>
                              <vxe-table-column field="colcode" title="条件字段代码" width="98" align="center" :edit-render="{ name: 'input', autoselect: true, events: { change: hasChange } }"></vxe-table-column>
                              <vxe-table-column field="colname" title="条件字段名称" width="98" header-align="center" :edit-render="{ name: 'input', autoselect: true, events: { change: hasChange } }"></vxe-table-column>
                              <vxe-table-column field="valuetype" title="取值类别" width="80" align="center" :edit-render="{ name: 'select', options: $opt.valuetypeOption, events: { change: valuetypechange } }"></vxe-table-column>
                              <vxe-table-column field="valuelist" title="取值列表(双击)" width="200" header-align="center" :edit-render="{ name: 'input', autoselect: true, events: { change: hasChange } }">
                                  <template v-slot:edit ="{ row }">
                                      <el-input v-if= "row.valuetype != '12'" v-model="row.valuelist" @change="hasChange" clearable placeholder="双击开窗"></el-input>
                                      <el-input v-else v-model="row.valuelist" :readonly="true" @change="hasChange" placeholder="双击开窗"></el-input>
                                  </template>
                              </vxe-table-column>
                              <vxe-table-column field="sqllist" title="隐藏取值列" width="200" header-align="center" v-if="false"></vxe-table-column>
                              <vxe-table-column field="defaultvalue" title="默认值" width="120" header-align="center" align="center">
                                  <!--:edit-render="{name:'input', autoselect: true,events: { change: hasChange}}"-->
                                  <template v-slot="{ row }">
                                      <div v-if="row.valuetype == '0'">
                                          <!---代表时间输入框-->
                                          <div v-if="row.dateformat == '1'">
                                              <el-date-picker v-model="row.defaultvalue" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" @change="DateChange($event, row)" :picker-options="pickerOptions"> </el-date-picker>
                                          </div>
                                          <div v-else>
                                              <el-date-picker v-model="row.defaultvalue" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" @change="DateChange($event, row)" :picker-options="pickerOptions"> </el-date-picker>
                                          </div>
                                      </div>
                                      <div v-else-if="row.valuetype == '1'">
                                          <el-checkbox true-label="1" false-label="0" v-model="row.defaultvalue" @change="hasChange"></el-checkbox>
                                      </div>
                                      <div v-else-if="row.valuetype == '3'">
                                          <el-select v-model="row.defaultvalue" @change="hasChange">
                                              <el-option v-for="(item, index) in row.valuelist.split('|')" :key="index" :label="item.split(' ')[1]" :value="item.split(' ')[0]"></el-option>
                                          </el-select>
                                      </div>
                                      <div v-else-if="row.valuetype == '12'">
                                          <el-select v-model="row.defaultvalue" @change="hasChange" clearable filterable placeholder="请选择">
                                              <el-option v-for="(item, index) in row.sqllist" :key="index" :label="item.display" :value="item.data"></el-option>
                                          </el-select>
                                      </div>
                                      <div v-else-if="row.valuetype == '7'">
                                          <rm-ddl-rptunit prgno="MNG123" v-model="row.defaultvalue"></rm-ddl-rptunit>
                                      </div>
                                      <div v-else-if="row.valuetype == '9'">
                                          <el-input v-model="row.defaultvalue" @dblclick.native="daterangeDbl(row)" placeholder="双击开窗"></el-input>
                                          <!-- <el-select v-model="row.defaultvalue" @change="hasChange" clearable>
                                              <el-option label="当天" value="1"></el-option>
                                              <el-option label="月初" value="2"></el-option>
                                              <el-option label="月末" value="3"></el-option>
                                          </el-select> -->
                                      </div>
                                      <div v-else>
                                          <el-input v-model="row.defaultvalue" @input="hasChange" clearable @dblclick.native="MultiSelect(row)" placeholder="双击开窗"></el-input>
                                      </div>
                                  </template>
                              </vxe-table-column>
                              <vxe-table-column field="dateformat" title="时间显示格式" width="110" header-align="center" :edit-render="{ name: 'select', options: $opt.dateformatOption, events: { click: hasChange } }"></vxe-table-column>
                              <vxe-table-column field="showmode" title="显示格式" width="80" header-align="center" :edit-render="{ name: 'select', options: $opt.showMOdeOption, events: { click: hasChange } }"></vxe-table-column>
                              <vxe-table-column field="isaddquotes" title="" width="0" header-align="center">
                                  <template v-slot="{  }">
                                      <span></span>
                                  </template>
                              </vxe-table-column>
                          </vxe-table>
                         
                      </div>
                      <div style="font-size: 17px; margin-top: 6px; background-color: #fff; text-align: center; font-weight: bold;">
                                      <el-button @click="btnAddDetail" class="el-button--310">新增条件</el-button>
                                      <el-button @click="btnDeleteDetail" class="el-button--310">删除条件</el-button>
                          </div>
                  </rm-layout-top>
              </div>
          </template>
          <div style="height: 100%; width: 100%">
              <vxe-table :data.sync="lxStatReportData" :loading="left_loading" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" border height="auto" highlight-current-row highlight-hover-column highlight-hover-row row-header-overflow show-overflow="ellipsis" ref="lxTable" resizable size="mini" stripe :row-class-name="RowClassName" @current-change="currentChangeEvent" @cell-dblclick="DblclickOpen" :keyboard-config="keyConfig">
                  <vxe-table-column field="reportkind" title="报表类别" width="80" header-align="center"></vxe-table-column>
                  <vxe-table-column field="reportname" title="报表名称" width="100" header-align="center"></vxe-table-column>
                  <vxe-table-column field="prgcode" title="所属菜单" width="80" header-align="center" align="center"></vxe-table-column>
                  <vxe-table-column field="reportsql" title="统计SQL(双击查看)" width="300" header-align="center"></vxe-table-column>
                  <vxe-table-column field="description" title="报表说明(双击查看)" width="117" header-align="center"></vxe-table-column>
                  <vxe-table-column field="connectionname" title="连接配置" width="117" header-align="center"></vxe-table-column>
                  <vxe-table-column field="seq" title="序号" width="50" align="center"></vxe-table-column>
                  <vxe-table-column field="statreportid" title="统计报表ID" width="79" align="center"></vxe-table-column>
              </vxe-table>
          </div>
      </rm-layout-right>

      <!--双击查看内容-->
      <el-dialog :title="RecTitle" :visible.sync="dialogmsg" width="50%" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false">
          <div style="width: 100%; height: 380px; overflow: auto; border: 3px double #ccc; padding: 10px">
              {{ RecContents }}
          </div>
      </el-dialog>
      <!--sql测试弹窗-->
      <el-dialog width="900px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="SQL语句测试" :visible.sync="dialogSqlVisible" :destroy-on-close="true" v-if="dialogSqlVisible">
          <el-input v-model="sqlstr" type="textarea" :rows="20"></el-input>
          <div style="text-align: right; width: 100%; margin-top: 5px">
              <el-button @click="TestSql(2)">查看sql</el-button>
              <el-button @click="TestSql(1)">测试sql</el-button>
              <el-button @click="(Form_StatReport.reportsql = sqlstr), (dialogSqlVisible = false), (changeflag = true)">确 定</el-button>
              <el-button @click="dialogSqlVisible = false">取 消</el-button>
          </div>
      </el-dialog>

      <el-dialog width="900px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :title="multichktitle" :visible.sync="dialogMultipleVisible" :destroy-on-close="true" v-if="dialogMultipleVisible">
          <rm-SqlMultiple :leftOption="LeftListOptions" :rightOption="RightListOptions" :name="fieldname" @change="closedialog"></rm-SqlMultiple>
      </el-dialog>

      <!--双击编辑valueList内容-->
      <el-dialog title="编辑取值来源" :visible.sync="dialogvaluelist" width="300px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false">
          <div v-if="valuelisttype == '1'">
              <vxe-table :data.sync="valuelistdata" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" :edit-config="{ trigger: 'click', mode: 'cell', activeMethod: activeMethod, showStatus: true, showIcon: false }" border height="350px" highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow ref="valuelistTable" resizable show-overflow size="mini" stripe :keyboard-config="keyConfig">
                  <vxe-table-column field="data" title="代码" width="80" header-align="center"></vxe-table-column>
                  <vxe-table-column field="title" title="名称" width="100" header-align="center" :edit-render="{ name: 'select', options: $opt.DateSelectOption }"></vxe-table-column>
              </vxe-table>
          </div>
          <div v-if="valuelisttype == '3'">
              <el-input type="textarea" rows="10" v-model="valuelistsql"></el-input>
          </div>
          <div v-else>
              <vxe-table :data.sync="valuelistdata" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" :edit-config="{ trigger: 'click', mode: 'cell', activeMethod: activeMethod, showStatus: true, showIcon: false }" border height="350px" highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow ref="valuelistTable" resizable show-overflow size="mini" stripe :keyboard-config="keyConfig">
                  <vxe-table-column field="data" title="代码" width="80" header-align="center" :edit-render="{ name: 'input' }"></vxe-table-column>
                  <vxe-table-column field="title" title="名称" width="100" header-align="center" :edit-render="{ name: 'input' }"></vxe-table-column>
              </vxe-table>
          </div>
          <span slot="footer" class="dialog-footer">
              <el-checkbox style="float: left;" v-if="valuelisttype == '3'" v-model="isAddQuotes" true-label="1" false-label="0">结果添加单引号</el-checkbox>
              <el-button v-if="valuelisttype != '1' && valuelisttype != '3'" @click="addLine">新增</el-button>
              <el-button v-if="valuelisttype != '1' && valuelisttype != '3'" @click="delLine">删除</el-button>
              <el-button v-if="valuelisttype == '1'" @click="SetUserDefineTime">设置自定义时间</el-button>
              <el-button type="primary" @click="SaveValuelist">确 定</el-button>
              <el-button @click="dialogvaluelist = false">取 消</el-button>
          </span>
      </el-dialog>

      <!--双击编辑挂靠菜单-->
      <el-dialog title="挂靠菜单" :visible.sync="dialogmenuVisible" width="50%" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" v-if="dialogmenuVisible">
          <vxe-table :data.sync="MenuData" :tree-config="{ children: 'subs' }" :select-config="{ reserve: true }" :sort-config="{ trigger: 'cell' }" border height="auto" highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow ref="MenuTable" resizable show-overflow size="mini" stripe :row-class-name="RowClassName" @current-change="currentMenuChangeEvent" :keyboard-config="keyConfig">
              <vxe-table-column field="title" title="标题" width="240px" header-align="center" tree-node></vxe-table-column>
          </vxe-table>
          <div style="width: 100%; text-align: right">
              <el-button @click="MenuSubmit">确 定</el-button>
              <el-button @click="dialogmenuVisible = false">取 消</el-button>
          </div>
      </el-dialog>

      <el-dialog width="900px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="查看SQL语句" :visible.sync="dialogSqlShowVisible" :destroy-on-close="true" v-if="dialogSqlShowVisible">
          <el-input v-model="sqlstrshow" type="textarea" :rows="20" readonly></el-input>
          <div style="text-align: right; width: 100%; margin-top: 5px">
              <el-button @click="dialogSqlShowVisible = false">关 闭</el-button>
          </div>
      </el-dialog>
      <!--导入全部确认-->
      <el-dialog width="800px" :append-to-body="true" :close-on-click-modal="false" title="文字编辑" :visible.sync="uploadpasswordVisible" v-if="uploadpasswordVisible">
          <div style="font-size:12px;padding: 0 0 0 5px;text-align:right;">
              <el-radio-group v-model="isoverride" style="margin-right: 5px;">
                  <el-radio :label="0" selected size="mini">直接覆盖原来的报表(根据报表名称)</el-radio>
                  <el-radio :label="1" size="mini">不覆盖，直接追加新的报表</el-radio>
                  <el-radio :label="2" size="mini">不覆盖，全部删除后再导入</el-radio>
              </el-radio-group>
              <el-button @click="uploadpasswordSure()">确定</el-button>
              <el-button @click="uploadpasswordVisible = false;">取消</el-button>
          </div>
          <div style="margin-top: 5px;height: 50px;">
              <el-input ref="uploadpasswordinput" placeholder="请输入'导入'两字" v-model="uploadpassword" style="height: 50px;"></el-input>
          </div>
      </el-dialog>
      <el-dialog title="文件导入提示" :visible.sync="fileimportnotifyVisible" width="30%">
          <el-form :model="fileimportform">
              <el-form-item label="上传文件" :label-width="formLabelWidth">
                  <el-upload ref="uploadDat" action="" :limit=limitNum :auto-upload="false" accept=".dat" :before-upload="beforeUploadDatFile" :on-change="fileimportChange" :on-exceed="exceedFile" :file-list="fileimportform.fileList" v-show="importtype == 0">
                      <el-button size="small" plain>选择文件</el-button>
                      <div slot="tip" class="el-upload__tip">只能上传一个DAT(*dat)文件，最大不超过10M。</div>
                  </el-upload>
                  <el-upload ref="uploadDat" action="" :limit=limitNum :auto-upload="false" accept=".repx" :multiple="true" :before-upload="beforeUploadRepxFile" :before-remove="handleFileRemove" :on-change="fileimportChange" :on-exceed="exceedFile" :file-list="fileimportform.fileList" v-show="importtype == 1">
                      <el-button slot="trigger" size="small" type="primary">批量选取文件</el-button>
                      <div>能上传多个REPX(*repx)文件，最多99个。</div>
                  </el-upload>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
              <el-button size="small" type="primary" @click="uploadFile()">立即上传</el-button>
              <el-button @click="fileimportnotifyVisible = false">取 消</el-button>
          </span>
      </el-dialog>
      <el-dialog width="600px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="设置自定义时间" :visible.sync="userdefinetimeVisible" :destroy-on-close="true" v-if="userdefinetimeVisible">
          <span>以当前日期往前推 </span>
          <el-input v-model="userdefinetime" ref="userdefinetimeinput" style="width:100px;" placeholder="默认为7"></el-input>
          <span> 天</span>
          <div style="text-align: right; width: 100%; margin-top: 5px">
              <el-button @click="userdefinetimeConfirm">确 定</el-button>
              <el-button @click="userdefinetimeVisible = false">关 闭</el-button>
          </div>
      </el-dialog>
  </rm-layout-top>
</template>

<script type="text/javascript">
import axios from "axios";
import XEUtils from "xe-utils";
export default {
  name: "MNG123",

  data() {
      var _this = this;
      return {
          searchobj: { prgcode: "", reportname: "" },
          rxConditionData: [],
          prgcodeOption: [],
          reportkindOption: [],
          datebaseConfigOption: [],
          rptunitOption: [],
          preCondData: [],
          OldPreCondData: [],
          lxStatReportData: [],
          lxStatReportTotalData: [],
          ShowInMenu: false,
          ShowExchange: false,

          left_loading: false,
          right_loading: false,
          dialogmsg: false,
          loading: false,
          RecContents: "",
          RecTitle: "",
          userno: this.$tool.getSession(this.$tool.loginuserno),
          Form_StatReport: {},
          oldForm_StatReport: {},
          rxTableRow: {},
          lxTableRow: {},
          nodata: true,
          utype: "M",
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: false,
          },
          AddData: [],
          ModData: [],
          secsysprgOption: [],
          OrisecsysprgOption: [],
          dialogvaluelist: false,
          valuelistContents: "",
          valuelistdata: [],
          valuelistsql: "",
          valuelisttype: "",
          changeflag: false,
          pickerOptions: {
              shortcuts: [
                  {
                      text: "当天",
                      onClick(picker) {
                          picker.$emit("pick", new Date());
                          _this.datepickflag = 1;
                      },
                  },
                  {
                      text: "当月初",
                      onClick(picker) {
                          const date = new Date();
                          //date.setTime(date.getTime() - 3600 * 1000 * 24);
                          date.setDate(1);
                          picker.$emit("pick", date);
                          _this.datepickflag = 2;
                      },
                  },
                  {
                      text: "当月末",
                      onClick(picker) {
                          const date = new Date();
                          //date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                          var fullYear = date.getFullYear();
                          var month = date.getMonth() + 1;
                          var endOfMonth = new Date(fullYear, month, 0).getDate();
                          date.setDate(endOfMonth);
                          picker.$emit("pick", date);
                          _this.datepickflag = 3;
                      },
                  },
              ],
          },
          datepickflag: 0,
          //挂靠菜单
          dialogmenuVisible: false,
          MenuData: [],
          //多选弹窗
          dialogMultipleVisible: false,
          multichktitle: "",
          LeftListOptions: [],
          RightListOptions: [],
          fieldname: "",
          ChangeRow: {},
          //sql测试弹窗
          sqlstr: "",
          dialogSqlVisible: false,
          sqlstrshow: "",
          dialogSqlShowVisible: false,
          isoverride: 0,
          uploadpassword: "",//项目导入口令
          uploadpasswordVisible: false,//项目导入口令确认弹窗
          limitNum: 1,
          formLabelWidth: "100px",
          fileimportform: {
              file: "",
              fileList:[],
              fileName: [],
          },
          fileimportnotifyVisible: false,
          importtype: 0,
          isAddQuotes: "0",
          userdefinetime: "",
          userdefinetimeVisible: false,
      };
  },
  created() {
      this.initOption(); //初始化下拉框的数据
      this.InitPage(); //初始化主列表
  },
  methods: {
      //下拉数据初始化
      initOption() {
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "initOption",
                  },
              })
              .then((res) => {
                  if (!res.hasOwnProperty("errcode")) {
                      this.prgcodeOption = res.prgcodeOption;
                      this.secsysprgOption = res.secsysprgOption;
                      this.OrisecsysprgOption = res.secsysprgOption;
                      this.reportkindOption = res.reportkindOption;
                      this.datebaseConfigOption = res.datebaseConfigOption;
                      this.rptunitOption = res.rptunitOption;
                      this.userdefinetime = res.userdefinetime;
                      if(this.userdefinetime) {
                          this.$opt.DateSelectOption.forEach((item) => {
                              if(item.value == "4") item.label = this.userdefinetime + "天前(自定义)";
                          });
                      }

                      if (res.mac == "10.0.188.193") this.ShowExchange = true;
                  }
              })
              .catch((e) => {
                  this.$alert(e, "error");
              });
      },
      reportkindInit() {
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "reportkindInit",
                  },
              })
              .then((res) => {
                  this.reportkindOption = res.reportkindOption;
              })
              .catch((e) => {
                  this.$alert(e, "error");
              });
      },
      btnQuery() {
          this.InitPage();
      }, //页面初始化
      InitPage() {
          this.left_loading = true;
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "InitPage",
                      ...this.searchobj,
                  },
              })
              .then((res) => {
                  console.log("data", res);
                  //this.lxStatReportData = res.Data;
                  this.lxStatReportTotalData = XEUtils.clone(res.Data, true);
                  this.DataFilter();
                  this.MenuData = [];
                  if (res.menu) {
                      for (let i = 0; i < res.menu.length; i++) {
                          this.MenuData.push({ title: res.menu[i].Item1, subs: res.menu[i].Item2 });
                      }
                  }
                  if (res.Data.length > 0) {
                      if (this.Form_StatReport.hasOwnProperty("statreportid") && this.Form_StatReport.statreportid) {
                          let row = this.lxStatReportTotalData.find((item) => item.statreportid == this.Form_StatReport.statreportid);
                          if (row) {
                              this.$refs.lxTable.setCurrentRow(row);
                              this.queryRight(row);
                          } else {
                              this.$refs.lxTable.setCurrentRow(res.Data[0]);
                              this.queryRight(res.Data[0]);
                          }
                      } else {
                          this.$refs.lxTable.setCurrentRow(res.Data[0]);
                          this.queryRight(res.Data[0]);
                      }
                      this.nodata = false;
                  } else {
                      this.$refs.lxTable.setCurrentRow(null);
                      this.rptstylelist = {};
                      this.selrow = [];
                      this.preCondData = [];
                      this.OldPreCondData = [];
                      this.nodata = true;
                  }
                  this.left_loading = false;
              })
              .catch((e) => {
                  this.left_loading = false;
                  this.$alert(e);
              });
      }, //右边查询
      queryRight(row) {
          this.lxTableRow = row;
          this.statreportid = row.statreportid;
          this.utype = "M";
          this.right_loading = true;
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "queryRight",
                      statreportid: row.statreportid,
                  },
              })
              .then((res) => {
                  if (res.dc_statreport.length > 0) {
                      this.Form_StatReport = res.dc_statreport[0];
                      this.oldForm_StatReport = XEUtils.clone(res.dc_statreport[0], true);
                  }
                  this.$refs.input3.focus();
                  this.rxConditionData = res.rxConditionData;
                  if(!!this.rxConditionData){
                      for (let index = 0; index < this.rxConditionData.length; index++) {
                          const element = this.rxConditionData[index];
                          if(element.valuetype=="12"){
                              //请求后台，获取下拉框数据
                              this.$tool
                                  .request({
                                      url: "MNG/MNG1/MNG123",
                                      method: "post",
                                      data: {
                                          action: "getoptionlist",
                                          sqlstr: this.rxConditionData[index].valuelist,
                                      },
                                  })
                                  .then((res) => {
                                      if (res.flag == "1") {
                                          this.rxConditionData[index].sqllist = res.data;
                                          this.$refs.rxTable.refreshData();
                                      } else {
                                          this.$message({
                                              message: res.msg,
                                              type: "warning",
                                          });
                                      }
                                  });
                          }
                      }
                  }
                  this.right_loading = false;
              })
              .catch((e) => {
                  this.right_loading = false;
                  this.$alert(e);
              });
      },
      //行切换
      currentChangeEvent({ row, column }) {
          if (this.changeflag) {
              this.$refs.lxTable.setCurrentRow(this.lxTableRow);
              return false;
          } else {
              this.queryRight(row);
          }
      },
      currentMenuChangeEvent({ row, column }) {
          //console.log(row);
      },
      //下拉回车
      selectBlur() {
          let query = this.$refs.ddlprgcodeSelect.selectedLabel;
          var re = /^[a-zA-Z]+.?[0-9]*$/;
          if (!re.test(query)) {
              //不是数字,暂时不做任何事
          } else {
              //是LIS640这种形式
              if (query !== "") {
                  let data = this.prgcodeOption.find((m) => m.value.toUpperCase() == query.toUpperCase());
                  if (!!data) {
                      this.searchobj.prgcode = data.value;
                  } else {
                      this.searchobj.prgcode = query;
                  }
                  this.$refs.ddlprgcodeSelect.blur();
              }
          }
      }, //双击查看
      DblclickOpen({ row, column }) {
          if (["reportsql", "description", "prestatsql"].includes(column.property)) {
              this.RecTitle = column.title.split("(")[0];
              this.RecContents = row[column.property];
              this.dialogmsg = true;
          } else if (column.property == "valuelist") {
              if (row.valuetype == "0") {
                  this.$alert("时间框的取值列表禁止手动编辑！", "提示");
              } else if (row.valuetype == "9") {
                  this.valuelisttype = "1";
                  this.valuelistdata = [];
                  this.rxTableRow = row;
                  var list = [];
                  if (row.valuelist) list = row.valuelist.split("|");
                  var colcodelist = row.colcode.split("-");
                  this.valuelistdata.push({ data: colcodelist[0], title: list.length > 0 ? list[0] : "" });
                  this.valuelistdata.push({ data: colcodelist[1], title: list.length > 1 ? list[1] : "" });
                  this.dialogvaluelist = true;
              } else if (row.valuetype == "3" || row.valuetype == "4") {
                  this.valuelisttype = "2";
                  this.valuelistdata = [];
                  this.rxTableRow = row;
                  var list = row.valuelist.split("|");
                  if (list.length > 0) {
                      for (let i = 0; i < list.length; i++) {
                          var onelist = list[i].split(" ");
                          this.valuelistdata.push({ data: onelist[0], title: onelist[1] });
                      }
                  }
                  this.dialogvaluelist = true;
              } else if (row.valuetype == "11" || row.valuetype == "12") {
                  this.valuelisttype = "3";
                  this.valuelistsql = row.valuelist;
                  if(row.valuetype == "11"){
                      this.isAddQuotes = row.isaddquotes == '1' ? row.isaddquotes : '0';
                  }
                  this.rxTableRow = row;
                  this.dialogvaluelist = true;
              } else {
              }
          }
      },
      delLine() {
          let delrow = this.$refs.valuelistTable.getCurrentRow();
          if (delrow) {
              var alld = this.$refs.valuelistTable.tableData;
              var rowindex = alld.indexOf(delrow);
              this.$refs.valuelistTable.remove(delrow);
              let havedata = this.$refs.valuelistTable.tableData;
              if (havedata.length > 0) {
                  if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                  this.tempindex = rowindex;
                  let selectrow = havedata[rowindex];
                  this.$refs.valuelistTable.setCurrentRow(selectrow);
              }
          } else {
              this.$message({
                  message: "请选择要删除的行！",
                  type: "warning",
              });
          }
      },
      addLine(){
          let initdata = {
              data: "",
              title: "",
          };
          this.$refs.valuelistTable.insert(initdata).then(({ row }) => {
              this.$refs.valuelistTable.setCurrentRow(row);
              this.$refs.valuelistTable.setActiveRow(row, "data");
          });
          this.FunscrollY();
      },
      FunscrollY() {
          this.$nextTick(() => {
              var alldataLen = this.$refs.valuelistTable.tableData.length;
              let L = this.$refs.valuelistTable.getTableData().fullData[alldataLen - 1];
              this.$refs.valuelistTable.scrollToRow(L);
          });
      },
      //valuelist取值
      SaveValuelist() {
          if (this.rxTableRow.valuetype == "9") {
              var text = "";
              for (let i = 0; i < this.$refs.valuelistTable.tableFullData.length; i++) {
                  text += this.$refs.valuelistTable.tableFullData[i].title + "|";
              }
              if (text.length > 1) this.rxTableRow.valuelist = text.substring(0, text.length - 1);
          } else if (this.rxTableRow.valuetype == "3" || this.rxTableRow.valuetype == "4") {
              var text = "";
              for (let i = 0; i < this.$refs.valuelistTable.tableFullData.length; i++) {
                  text += this.$refs.valuelistTable.tableFullData[i].data + " " + this.$refs.valuelistTable.tableFullData[i].title + "|";
              }
              if (text.length > 1) this.rxTableRow.valuelist = text.substring(0, text.length - 1);
          }
          else if (this.rxTableRow.valuetype == "11") {
              this.rxTableRow.valuelist = this.valuelistsql;
              this.rxTableRow.isaddquotes = this.isAddQuotes;
          }
          else if (this.rxTableRow.valuetype == "12") {
              this.rxTableRow.valuelist = this.valuelistsql;
              var currentRow = this.$refs.rxTable.getCurrentRow();
              currentRow.valuelist = this.valuelistsql;
              //请求后台，获取下拉框数据
              this.$tool
                  .request({
                      url: "MNG/MNG1/MNG123",
                      method: "post",
                      data: {
                          action: "getoptionlist",
                          sqlstr: this.valuelistsql,
                      },
                  })
                  .then((res) => {
                      if (res.flag == "1") {
                          currentRow.sqllist = res.data;
                          this.$refs.rxTable.setActiveCell(currentRow,"valuelist");
                      } else {
                          this.$message({
                              message: res.msg,
                              type: "warning",
                          });
                      }
                  });
          }
          this.hasChange();
          this.dialogvaluelist = false;
      },
      //设置用户自定义时间
      SetUserDefineTime() {
          this.userdefinetimeVisible = true;
      },
      userdefinetimeConfirm() {
          if (!this.userdefinetime){
              this.$alert("请输入设置的天数！").then(()=>{ this.$refs.userdefinetimeinput.focus() });
              return;
          }
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "setuserdefinetime",
                      time: this.userdefinetime,
                  },
              })
              .then((res) => {
                  if (res.flag == "1") {
                      this.$opt.DateSelectOption.forEach((item) => {
                          if(item.value == "4") item.label = this.userdefinetime + "天前(自定义)";
                      });
                      this.$message.success("设置成功！");
                      this.userdefinetimeVisible = false;
                  } else {
                      this.$message.error("设置失败！");
                  }
              });
      },
      changecurrclick({row}){
          if(row.valuetype == "12"){
              //请求后台，获取下拉框数据
              this.$tool
                  .request({
                      url: "MNG/MNG1/MNG123",
                      method: "post",
                      data: {
                          action: "getoptionlist",
                          sqlstr: row.valuelist,
                      },
                  })
                  .then((res) => {
                      if (res.flag == "1") {
                          row.sqllist = res.data;
                          this.$refs.rxTable.setActiveCell(row,"sqllist");
                      } else {
                          this.$message({
                              message: res.msg,
                              type: "warning",
                          });
                      }
                  });
          }
      },
      //导出
      btnExport(type) {
          //if (typeof jsObj != "undefined") {
          if (this.statreportid == "") {
              this.$alert("没有获取到统计报表ID");
              return false;
          }

          //导出所有报表及报表样式
          if(type == "0"){
              let p = { prgno: "MNG123", rptstylename:"MNG123_自定义报表配置_全部报表", exporttype: type };
              this.$pub.commexport(this, p, `MNG123_自定义报表配置_全部报表.dat`);
              //this.$pub.commexport(this, p, `MNG123_自定义报表配置_全部报表样式.zip`);
              this.RepxBatchExport();
          }
          else{
              let p = { prgno: "MNG123", rptstyleid: this.Form_StatReport.statreportid, rptstylename: this.Form_StatReport.reportname, exporttype: type };
              this.$pub.commexport(this, p, this.Form_StatReport.reportname + (type == "1" ? `.repx` : `.dat`));
          }

          // } else {
          //     this.$alert("请使用" + this.$pub.clientname + "打开系统", "错误", {
          //         confirmButtonText: "确定"
          //     });
          // }
      },
      //报表样式批量导出
      RepxBatchExport(){
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  timeout: 1800000,
                  data: {
                      action: "repxFileTrans",
                  },
              })
              .then((res) => {
                  if (res.flag == "0") {
                      this.$alert(res.msg);
                  } else if (res.flag == "1") {
                      console.log(res.msg);
                      let p = {
                          prgno: "MNG123_batch",
                          filename: res.folderna,
                          zipPath: res.zipPath,
                          filesPath: res.filesPath,
                      };
                      this.$pub.commexport(this, p, res.folderna + ".zip");
                  }
              })
              .catch((e) => {
                  this.$alert("报表样式导出失败！"+ e, "错误", {
                      confirmButtonText: "确定",
                  });
              });
      },
      //导入
      btnImport(type) {
          if (type== "0") {
              this.importtype = 0;
              this.limitNum = 1;
              let msgtext = "导入可能导致当前系统所有报表丢失，你是否要继续？";
              this.$confirm(msgtext, "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
              }).then(() => {
                  this.uploadpasswordVisible = true;
                  this.uploadpassword = "";
                  })
                  .catch(() => {});
          } else if(type== "3") {
              this.importtype = 1;
              this.limitNum = 99;
              this.fileimportnotifyVisible = true;
          } else {
              this.$rm_uploadFiles({
                  type: "文件上传",
                  prgname: "MNG123",
                  param1: this.Form_StatReport.statreportid,
                  btn: {
                      confirmBtnText: "立即上传",
                      cancelBtnText: "取消",
                  },
              })
                  .then((res) => {
                      if (res.flag == "1") {
                          this.$pub.message(this, "文件上传成功", "success");
                          this.InitPage();
                      } else {
                          this.$pub.message(this, "文件上传失败", "error");
                      }
                  })
                  .catch((e) => {
                      console.log("error", e);
                  });
          }
      },
      uploadpasswordSure(){
          console.log(this.uploadpassword);
          if(this.uploadpassword == "导入") {
              this.uploadpasswordVisible = false;
              this.fileimportnotifyVisible = true;
          }
      },
      // 文件超出个数限制时的钩子
      exceedFile(files, fileList) {
          this.$notify.warning({
              title: "警告",
              message: `只能选择 ${this.limitNum} 个文件，当前共选择了 ${files.length + fileList.length} 个`
          });
      },
      // 文件状态改变时的钩子
      fileimportChange(file, fileList) {
          this.fileimportform.file = file.raw;
          this.fileimportform.fileList = fileList;
      },
      // 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject，则停止上传
      beforeUploadDatFile(file) {
          let extension = file.name.substring(file.name.lastIndexOf(".") + 1);
          let size = file.size / 1024 / 1024;
          if (extension !== "dat") {
              this.$notify.warning({
                  title: "警告",
                  message: `只能上传DAT(*dat)文件，且不超过10M`
              });
          }
          if (size > 10) {
              this.$notify.warning({
                  title: "警告",
                  message: `文件大小不得超过10M`
              });
          }
      },
      beforeUploadRepxFile(file) {
          let extension = file.name.substring(file.name.lastIndexOf(".") + 1);
          if (extension !== "repx") {
              this.$notify.warning({
                  title: "警告",
                  message: `只能上传REPX(*repx)文件`
              });
          }
      },
      // 删除之前钩子
      handleFileRemove(file, fileList) {
          this.fileimportform.fileList = fileList;
      },
      uploadFile() {
          if (this.fileimportform.file || this.fileimportform.fileList.length > 0) {
              var URL = this.$tool.baseUrl.replace("api", "z") + "fileupload";
              let formData = new FormData();
              let loadingtext = "";
              if (this.importtype == 0) {
                  formData.append("file", this.fileimportform.file);
                  formData.append("overridetype", this.isoverride);
                  loadingtext = "正在导入报表，请稍候……";
              } else {
                  // 将上传的文件放到数据对象中
                  this.fileimportform.fileList.forEach((file) => {
                      formData.append("file", file.raw);
                      this.fileimportform.fileName.push(file.name);
                  });
                  // 文件名
                  formData.append("fileName", this.fileimportform.fileName);
                  loadingtext = "正在导入报告单样式，请稍候……";
              }
              const loading = this.$loading({
                  // 声明一个loading对象
                  lock: true, // 是否锁屏
                  text: loadingtext, // 加载动画的文字
                  spinner: "el-icon-loading", // 引入的loading图标
                  background: "rgba(0, 0, 0, 0.3)", // 背景颜色
                  target: ".sub-main", // 需要遮罩的区域
                  body: true,
                  customClass: "mask", // 遮罩层新增类名
              });
              formData.append("action", "MNG123_batch");
              axios
                  .post(URL, formData, { "Content-Type": "multipart/form-data" })
                  .then(res => {
                      loading.close();
                      if (res.status == 200) {
                          let response = res.data;
                          if (response.flag == "1") {
                              this.$notify.success({
                                  title: "成功",
                                  message: `文件上传成功`,
                              });
                              this.fileimportnotifyVisible = false;
                              this.fileimportform.file = "";
                              this.fileimportform.fileList = [];
                              this.fileimportform.fileName = [];
                              this.InitPage();
                          } else {
                              this.$notify.error({
                                  title: "错误",
                                  message: `文件上传失败`,
                              });
                              this.fileimportnotifyVisible = true;
                          }
                      } else {
                          this.$notify.error({
                              title: "错误",
                              message: `文件上传失败`,
                          });
                          this.fileimportnotifyVisible = true;
                      }
                  })
                  .catch(e => {
                      console.log("error", e);
                  });
          } else {
              this.$alert("请先选择文件再上传！");
          }
      },
      //样式设置
      btnSetStyle() {
          if (typeof jsObj != "undefined") {
              if (this.statreportid == "") {
                  this.$alert("没有获取到统计报表ID");
                  return false;
              }
              //let sql = btoa(encodeURIComponent(this.Form_StatReport.reportsql));
              //jsObj.SetCustomPrintConfig(this.statreportid + "", sql, this.userno, "chartreport");
              //jsObj.SetCustomJsonPrintConfig(this.statreportid + "", this.Form_StatReport.reportsql, this.userno, "HIV统计样式");
              this.$tool
                  .request({
                      url: "MNG/MNG1/MNG123",
                      method: "post",
                      data: {
                          action: "adjustifyStatStyle",
                          statreportid: this.statreportid,
                      },
                  })
                  .then((res) => {
                      if (res.errcode == "1") {
                          //console.log("result", res);
                          //let sql = btoa(encodeURIComponent(res.statsql));
                          //jsObj.SetCustomPrintConfig(this.statreportid + "", sql, this.userno, this.Form_StatReport.reportname);
                          if (res.data.length > 0) {
                              let str = JSON.stringify(res.columns);
                              str += "\r";
                              str += JSON.stringify(res.data);
                              let sql = btoa(encodeURIComponent(str));
                              //console.log("data", sql);
                              jsObj.SetCustomJsonPrintConfig(this.statreportid + "", sql, this.userno, "report");
                          } else {
                              this.$alert("获取数据失败！", "提示");
                          }
                      } else {
                          this.$alert("数据读取失败！" + res.errmsg, "提示");
                      }
                  })
                  .catch((e) => {
                      this.$alert(e);
                  });
          } else {
              this.$alert("请使用" + this.$pub.clientname + "打开系统", "错误", {
                  confirmButtonText: "确定",
              });
          }
    },
    //新增条件
      btnAddDetail() {
          if (this.nodata) {
              //如果没有数据禁止添加明细，得先点击新增
              this.$alert("请先点击新增统计报表，然后新增统计条件！", "提示");
              return false;
          } else {
              if (!!this.Form_StatReport.statreportid) {
                  let len = this.$refs.rxTable.tableData.length + 1;
                  this.$refs.rxTable.insertAt({ seq: len * 10, statreportid: this.Form_StatReport.statreportid, condoperator: "", showmode: "1", colcode: "@" + len }, -1).then(({ row }) => {
                      this.$refs.rxTable.setCurrentRow(row);
                      this.$refs.rxTable.scrollToRow(row);
                      this.$refs.rxTable.setActiveCell(row, "colname");
                  });
                  this.changeflag = true;
              } else {
                  this.$alert("红字标识的部分为必填项！");
              }
          }
      }, //删除明细
      btnDeleteDetail() {
          let delrow = this.$refs.rxTable.getCurrentRow();
          if (delrow) {
              var alldatd = this.$refs.rxTable.getTableData().fullData;
              var rowindex = alldatd.indexOf(delrow);
              this.$refs.rxTable.remove(delrow);
              let havedata = this.$refs.rxTable.getTableData().fullData;
              if (havedata.length > 0) {
                  if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                  let selectrow = havedata[rowindex];
                  this.$refs.rxTable.setCurrentRow(selectrow);
                  setTimeout(() => {
                      this.$refs.rxTable.scrollToRow(selectrow);
                  }, 10);
              } else {
                  this.$refs.rxTable.setCurrentRow(null);
              }
              this.changeflag = true;
          }
      },
      //新增
      btnAdd() {
          this.nodata = false;
          this.rxConditionData = [];
          this.utype = "A";
          this.changeflag = true;
          this.verifyReportId()
              .then((res) => {
                  if (res.errcode == "0") {
                      this.$alert(res.errmsg, "提示");
                      this.Form_StatReport.statreportid = "";
                  } else {
                      this.Form_StatReport = {
                          prgcode: res.prgcode,
                          statreportid: res.errmsg,
                          seq: 10,
                      };
                      setTimeout(() => {
                          this.$refs.input3.focus();
                      }, 10);
                  }
              })
              .catch((e) => {
                  this.$alert(e, "出错");
              });
      },
      //验证统计报表ID是否合法
      verifyReportId() {
          return this.$tool.request({
              url: "MNG/MNG1/MNG123",
              method: "post",
              data: {
                  action: "verifyReportId",
              },
          });
      },
      //删除
      btnDelete() {
          try {
              let delrow = this.$refs.lxTable.getCurrentRow();
              if (delrow) {
                  let msg = "确定删除该条记录吗？";
                  if (delrow.reportname != "") {
                      msg = "确定删除报表名称为：<span style='color:red'>" + delrow.reportname + "</span>的记录吗？";
                  }
                  this.$confirm(msg, "提示", {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                      dangerouslyUseHTMLString: true,
                  })
                      .then(() => {
                          var alldatd = this.$refs.lxTable.getTableData().fullData;
                          var rowindex = alldatd.indexOf(delrow);
                          this.utype = "D";
                          this.saveRecord()
                              .then((res) => {
                                  if (res.errcode == "1") {
                                      this.$refs.lxTable.remove(delrow);
                                      this.$pub.message(this, "删除成功！", "success");
                                      let havedata = this.$refs.lxTable.getTableData().fullData;
                                      if (havedata.length > 0) {
                                          if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                                          let selectrow = havedata[rowindex];
                                          this.$refs.lxTable.setCurrentRow(selectrow);
                                          this.queryRight(selectrow);
                                          setTimeout(() => {
                                              this.$refs.lxTable.scrollToRow(selectrow);
                                          }, 10);
                                      } else {
                                          this.$refs.lxTable.setCurrentRow(null);
                                          this.Form_StatReport = {};
                                          this.rxConditionData = [];
                                          this.nodata = true;
                                      }
                                  } else {
                                      this.$alert("数据读取失败！" + res.errmsg, "提示");
                                  }
                              })
                              .catch((e) => {
                                  this.$alert(e);
                              });
                      })
                      .catch(() => {});
              }
          } catch (e) {
              console.log("error", e);
          }
      },
      //保存
      btnSave() {
          this.saveRecord()
              .then((res) => {
                  if (res.errcode == "1") {
                      this.changeflag = false;
                      this.$pub.message(this, "保存成功！", "success");
                      if (this.utype == "A") {
                          if (this.AddData.indexOf(res.keyid) == -1) {
                              this.AddData.push(res.keyid + "");
                          }
                          this.utype = "M";
                          this.$refs.lxTable
                              .insertAt(
                                  {
                                      statreportid: this.Form_StatReport.statreportid,
                                      prgcode: this.Form_StatReport.prgcode,
                                      reportkind: this.Form_StatReport.reportkind,
                                      reportname: this.Form_StatReport.reportname,
                                      reportsql: this.Form_StatReport.reportsql,
                                      description: this.Form_StatReport.description,
                                      connectioncode: this.Form_StatReport.connectioncode,
                                      seq: this.Form_StatReport.seq,
                                  },
                                  -1
                              )
                              .then(({ row }) => {
                                  this.$refs.lxTable.setCurrentRow(row);
                                  this.$refs.lxTable.scrollToRow(row);
                                  this.queryRight(row);
                              });
                      } else if (this.utype == "M") {
                          if (this.ModData.indexOf(res.keyid) == -1) {
                              this.ModData.push(res.keyid + "");
                          }
                          this.lxTableRow.statreportid = this.Form_StatReport.statreportid;
                          this.lxTableRow.prgcode = this.Form_StatReport.prgcode;
                          this.lxTableRow.reportkind = this.Form_StatReport.reportkind;
                          this.lxTableRow.reportname = this.Form_StatReport.reportname;
                          this.lxTableRow.reportsql = this.Form_StatReport.reportsql;
                          this.lxTableRow.description = this.Form_StatReport.description;
                          this.lxTableRow.connectioncode = this.Form_StatReport.connectioncode
                          this.lxTableRow.seq = this.Form_StatReport.seq;
                          setTimeout(() => {
                              this.$refs.lxTable.setCurrentRow(this.lxTableRow);
                              this.queryRight(this.lxTableRow);
                          }, 10);
                      }
                  } else {
                      this.$alert("数据读取失败！" + res.errmsg, "提示");
                  }
              })
              .catch((e) => {
                  this.$alert(e);
              });
      },
      saveRecord() {
          return this.$tool.request({
              url: "MNG/MNG1/MNG123",
              method: "post",
              data: {
                  action: "SaveRecord",
                  newinfo: this.Form_StatReport,
                  oldinfo: this.oldForm_StatReport,
                  condition: this.$refs.rxTable.getRecordset(),
                  utype: this.utype,
              },
          });
      },
      btnCancel() {
          let selectRow = this.$refs.lxTable.getCurrentRow();
          if (selectRow) {
              this.queryRight(selectRow);
          } else {
              this.Form_StatReport = {};
              this.rxConditionData = [];
          }
          this.changeflag = false;
      },
      // 回车切换输入框
      nextFocus(index) {
          if (index > 9) return false;
          var that = this;
          var focusIndex = index + 1;
          setTimeout(function () {
              if (index == 3 || index == 9) {
                  that.$refs["input" + index].suggestions = [];
                  that.$refs["input" + focusIndex].focus(); //所属菜单为disabled
                  that.$refs["input" + focusIndex].select();
              } else {
                  that.$refs["input" + focusIndex].focus();
                  if (focusIndex == "5") {
                      that.$refs["input" + focusIndex].select();
                  }
              }
          }, 1);
      }, //控制是否可以编辑
      activeMethod({ row, column }) {
          if (column.property == "dateformat") {
              if (row.valuetype == "0" || row.valuetype == "9") {
                  return true;
              } else {
                  return false;
              }
          }
          return true;
      }, //控制右下不可编辑单元格的样式
      cellClassName({ row, column }) {
          if (column.property == "valuelist") {
              if (row.valuetype == "0") {
                  //标识时间没有取值列表
                  return this.$pub.diseditcolumn();
              } else {
                  return "";
              }
          } else {
              if (column.property == "dateformat") {
                  if (row.valuetype == "0") {
                      //标识时间有时间显示格式
                      if (row.defaultvalue == null) {
                          row.defaultvalue = new Date();
                      }

                      return "";
                  } else {
                      return this.$pub.diseditcolumn();
                  }
              }
          }
          return "";
      },
      //报表类型回调
      querySearch(queryString, cb) {
          var results = this.reportkindOption;
          cb(results);
      },
      //行颜色
      RowClassName({ row, rowIndex }) {
          if (this.AddData.indexOf(row.statreportid + "") > -1) {
              return "row-new";
          }
          if (this.ModData.indexOf(row.statreportid + "") > -1) {
              return "row-modify";
          }
      },
      hasChange() {
          this.changeflag = true;
      },
      valuetypechange(scope, val) {
          this.changeflag = true;
          scope.row.valuetype = val.target.value;
          if (scope.row.valuetype == "9") {
              let num = this.$refs.rxTable.tableFullData.filter((item) => item.valuetype == "9").length;
              scope.row.colcode = "@sdt" + num + "-@edt" + num;
              scope.row.valuelist = "1|1";
              cope.row.colname = "";
          } else if (scope.row.valuetype == "7" || scope.row.valuetype == "8") {
              scope.row.colname = "报告单元";
              scope.row.valuelist = "";
          } else if (scope.row.valuetype == "3") {
              scope.row.colname = "";
              scope.row.valuelist = "主键1 显示文字1|主键2 显示文字2";
          } else if (scope.row.valuetype == "11") {
              scope.row.colname = "";
              scope.row.valuelist = "select '主键' as data ,'显示文字' as title ,'1' as inputcode ";
          } else if (scope.row.valuetype == "12") {
              scope.row.colname = "";
              scope.row.valuelist = "select '主键' as data ,'显示文字' as display ,'1' as inputcode ";
              scope.row.sqllist= [{data:'主键',title:'显示文字'}];
          } else {
              scope.row.valuelist = "";
              //scope.row.defaultvalue = new date();
          }
          scope.row.defaultvalue = null;
      },
      DateChange(val, row) {
          if (this.datepickflag > 0) {
              switch (this.datepickflag) {
                  case 1:
                      row.valuelist = "1 当天";
                      break;
                  case 2:
                      row.valuelist = "2 当月初";
                      break;
                  case 3:
                      row.valuelist = "3 当月末";
                      break;
              }
              this.datepickflag = 0;
          } else {
              row.valuelist = "";
          }
          this.changeflag = true;
      },
      CondRowClassName({ row }) {
          let isDirty = this.$refs.rxTable.hasRowChange(row);
          if (isDirty) {
              return "row-modify";
          }
          if (this.$refs.rxTable.hasRowInsert(row)) return "row-new";
      }, //所属菜单
      remoteMethod(query) {
          if (query !== "") {
              this.loading = true;
              setTimeout(() => {
                  this.loading = false;
                  this.secsysprgOption = this.OrisecsysprgOption.filter((item) => {
                      return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1 || item.value.toLowerCase().indexOf(query.toLowerCase()) > -1;
                  });
              }, 200);
          } else {
              this.secsysprgOption = this.OrisecsysprgOption;
          }
      },
      MenuSubmit() {
          let row = this.$refs.MenuTable.getCurrentRow();
          console.log("msubmit", row);
          if (!row.subs) {
              this.Form_StatReport.parentcode = row.title.slice(0, 5);
              this.dialogmenuVisible = false;
              this.changeflag = true;
          } else {
              this.$alert("请选择到最底层菜单!");
          }
      },
      MultiSelect(row) {
          var rowlist = [];
          if (row.defaultvalue) rowlist = row.defaultvalue.split(",");
          this.multichktitle = row.colname;
          this.LeftListOptions = [];
          this.RightListOptions = [];
          this.ChangeRow = row;
          if (row.valuetype == "4") {
              var alloption = [];
              var alloptionlist = row.valuelist.split("|");
              for (let i = 0; i < alloptionlist.length; i++) {
                  alloption.push({ data: alloptionlist[i].split(" ")[0], display: alloptionlist[i].split(" ")[1], inputcode: "" });
              }
              if (rowlist.length > 0) {
                  this.LeftListOptions = alloption.filter((item) => rowlist.indexOf(item.data) == -1);
                  this.RightListOptions = alloption.filter((item) => rowlist.indexOf(item.data) > -1);
              } else {
                  this.LeftListOptions = alloption;
                  this.RightListOptions = [];
              }
              this.dialogMultipleVisible = true;
          } else if (row.valuetype == "8") {
              this.LeftListOptions = this.rptunitOption.filter((item) => rowlist.indexOf(item.data + "") == -1);
              this.RightListOptions = this.rptunitOption.filter((item) => rowlist.indexOf(item.data + "") > -1);
              this.dialogMultipleVisible = true;
          } else if (row.valuetype == "11") {
              var alloption = [];
              //请求后台
              this.$tool
                  .request({
                      url: "MNG/MNG1/MNG123",
                      method: "post",
                      data: {
                          action: "getoptionlist",
                          sqlstr: row.valuelist,
                      },
                  })
                  .then((res) => {
                      if (res.flag == "1") {
                          var alloptionlist = res.data;
                          console.log(alloptionlist);
                          for (let i = 0; i < alloptionlist.length; i++) {
                              alloption.push({ data: alloptionlist[i].data, display: alloptionlist[i].title, inputcode: alloptionlist[i].inputcode });
                          }
                          if (rowlist.length > 0) {
                              this.LeftListOptions = alloption.filter((item) => rowlist.indexOf(item.data) == -1);
                              this.RightListOptions = alloption.filter((item) => rowlist.indexOf(item.data) > -1);
                          } else {
                              this.LeftListOptions = alloption;
                              this.RightListOptions = [];
                          }
                          this.dialogMultipleVisible = true;
                      } else {
                          this.$message({
                              message: res.msg,
                              type: "warning",
                          });
                      }
                  });
          }
      },
      closedialog(value, code, valuename) {
          if (value != "close") {
              this.ChangeRow.defaultvalue = value;
              this.changeflag = true;
          }
          this.dialogMultipleVisible = false;
      },
      //过滤
      DataFilter() {
          this.lxStatReportData = this.lxStatReportTotalData.filter((item) => (this.searchobj.reportkind ? item.reportkind == this.searchobj.reportkind : true) && ((item.prgcode ? item.prgcode.toUpperCase().indexOf(this.searchobj.reportname.toUpperCase()) > -1 : false) || (item.reportkind ? item.reportkind.toUpperCase().indexOf(this.searchobj.reportname.toUpperCase()) > -1 : false) || (item.reportname ? item.reportname.toUpperCase().indexOf(this.searchobj.reportname.toUpperCase()) > -1 : false) || (item.description ? item.description.toUpperCase().indexOf(this.searchobj.reportname.toUpperCase()) > -1 : false)));
          this.$nextTick(() => {
              if (this.lxStatReportData.length > 0) {
                  var selrow = this.lxStatReportData.find((item) => item.statreportid == this.Form_StatReport.statreportid);
                  if (!selrow) {
                      this.$refs.lxTable.setCurrentRow(this.lxStatReportData[0]);
                      this.queryRight(this.lxStatReportData[0]);
                  } else {
                      this.$refs.lxTable.setCurrentRow(selrow);
                  }
              } else {
                  this.Form_StatReport = {};
              }
          });
      },
      daterangeDbl(row) {
          this.valuelisttype = "1";
          this.valuelistdata = [];
          this.rxTableRow = row;
          var list = [];
          if (row.valuelist) list = row.valuelist.split("|");
          var colcodelist = row.colcode.split("-");
          this.valuelistdata.push({ data: colcodelist[0], title: list.length > 0 ? list[0] : "" });
          this.valuelistdata.push({ data: colcodelist[1], title: list.length > 1 ? list[1] : "" });
          this.dialogvaluelist = true;
      },
      OpenTestSql() {
          this.sqlstr = XEUtils.clone(this.Form_StatReport.reportsql, true);
          this.dialogSqlVisible = true;
      },
      TestSql(type) {
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "TestSql",
                      sql: this.sqlstr,
                      urec: this.$refs.rxTable.getTableData().fullData,
                      statreportid: this.Form_StatReport.statreportid,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      if (type == "1") this.$alert("测试成功", "提示", { type: "success" });
                      else {
                          this.sqlstrshow = res.statsql;
                          this.dialogSqlShowVisible = true;
                      }
                  } else {
                      this.$alert(res.errmsg, "提示", { type: "error" });
                  }
              });
      },
      ExChangereportid(id, type) {
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG123",
                  method: "post",
                  data: {
                      action: "ExChangeToFixed",
                      statreportid: id,
                      type: type,
                  },
              })
              .then((res) => {
                  // if (!res.hasOwnProperty("errcode") && res.flag > 0) {
                  //     this.InitPage();
                  // } else {
                  //     this.$alert(res.errmsg, "错误");
                  // }
                  if (!res.hasOwnProperty("errcode"))
                      this.$confirm(res.sql, "sql语句").then(() => {
                          let p = { prgno: "MNG123", rptstyleid: this.Form_StatReport.statreportid, rptstylename: this.Form_StatReport.reportname, exporttype: "1" };
                          this.$pub.commexport(this, p, res.id + this.Form_StatReport.reportname + `.repx`);
                      });
              });
      },
  },
};
</script>

<style scoped>
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
  padding: 0px 2px 0px 5px;
}
.search-title {
  position: absolute;
}
.top-deal {
  right: 5px;
  position: absolute;
  top: 37px;
  display: flex;
  align-items: center;
}
::v-deep tr.vxe-body--row.row-new {
  color: #cc6600 !important;
}
::v-deep tr.vxe-body--row.row-modify {
  color: blue !important;
}
.el-input-number--small {
  width: 130px;
  line-height: 23px;
}
::v-deep .el-button.is-disabled {
  border: 0px;
  color: grey;
  background-color: #e6e6e6;
  font-weight: normal;
}
.btns {
  position: fixed;
  width: 81%;
  top: 88%;
  font-size: 17px;
  padding: 3px;
  background-color: #fff;
  text-align: center;
  font-weight: bold
}
</style>
